package com.tcm.day01;

/**
 * 前缀和
 */
public class Code03_prefixSum {

    public static int[] prefixSum(int[] arr) {

        int[] prefixSumArr = new int[arr.length];
        prefixSumArr[0] = arr[0];

        for (int i = 1; i < arr.length; i++) {
            prefixSumArr[i] = prefixSumArr[i - 1] + arr[i];
        }

        return prefixSumArr;
    }

    public static int subArr(int[] arr, int l, int r) {
        return l == 0 ? arr[r] : arr[r] - arr[l - 1];
    }

    public static void main(String[] args) {
        int[] arr = {5, 5, 1, 5, 8, 7, 3, 7};
        int[] sumArr = prefixSum(arr);
        int r = subArr(sumArr, 0, 2);
        System.out.println(r);
    }
}
